<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Simple Examples - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/learning.paginator.simple.html">Inglês (English)</a></li>
    <li><a href="../pt-br/learning.paginator.simple.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="learning.paginator.intro.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="learning.paginator.html">Getting Started with Zend_Paginator</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="learning.paginator.control.html">Pagination Control and ScrollingStyles</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="learning.paginator.simple" class="section"><div class="info"><h1 class="title">Simple Examples</h1></div>
    

    <p class="para">
        In this first example we won&#039;t do anything spectacular, but hopefully it will
        give you a good idea of what <span class="classname">Zend_Paginator</span> is designed to do.
        Let&#039;s say we have an array called $data with the numbers 1 to 100 in it, which
        we want to divide over a number of pages. We can use the static
         <span class="methodname">factory()</span> method in the <span class="classname">Zend_Paginator</span>
        class to get a <span class="classname">Zend_Paginator</span> object with our array in it.
    </p>

    <pre class="programlisting brush: php">
// Create an array with numbers 1 to 100
$data = range(1, 100);

// Get a Paginator object using Zend_Paginator&#039;s built-in factory.
$paginator = Zend_Paginator::factory($data);
</pre>


    <p class="para">
        We&#039;re already almost done! The $paginator variable now contains a reference to the
        Paginator object. By default it is setup to display 10 items per page.
        To display the items for the currently active page, all you need to do is iterate
        over the Paginator object with a foreach loop. The currently active page defaults
        to the first page if it&#039;s not explicitly specified. We will see how you can select
        a specific page later on. The snippet below will display an unordered list containing the
        numbers 1 to 10, which are the numbers on the first page.
    </p>

    <pre class="programlisting brush: php">
// Create an array with numbers 1 to 100
$data = range(1, 100);

// Get a Paginator object using Zend_Paginator&#039;s built-in factory.
$paginator = Zend_Paginator::factory($data);

?&gt;&lt;ul&gt;&lt;?php

// Render each item for the current page in a list-item
foreach ($paginator as $item) {
    echo &#039;&lt;li&gt;&#039; . $item . &#039;&lt;/li&gt;&#039;;
}

?&gt;&lt;/ul&gt;
</pre>


    <p class="para">
        Now let&#039;s try and render the items on the second page. You can use the
         <span class="methodname">setCurrentPageNumber()</span> method to select which page you want to
        view.
    </p>

    <pre class="programlisting brush: php">
// Create an array with numbers 1 to 100
$data = range(1, 100);

// Get a Paginator object using Zend_Paginator&#039;s built-in factory.
$paginator = Zend_Paginator::factory($data);

// Select the second page
$paginator-&gt;setCurrentPageNumber(2);

?&gt;&lt;ul&gt;&lt;?php

// Render each item for the current page in a list-item
foreach ($paginator as $item) {
    echo &#039;&lt;li&gt;&#039; . $item . &#039;&lt;/li&gt;&#039;;
}

?&gt;&lt;/ul&gt;
</pre>


    <p class="para">
        As expected, this little snippet will render an unordered list with the numbers
        11 to 20 in it.
    </p>

    <p class="para">
        These simple examples demonstrate a small portion of what can be achieved with
        <span class="classname">Zend_Paginator</span>. However, a real application rarely reads its data
        from a plain array, so the next section is dedicated to showing you how you can use
        Paginator to paginate the results of a database query. Before reading on, make sure you&#039;re
        familiar with the way <span class="classname">Zend_Db_Select</span> works!
    </p>

    <p class="para">
        In the database examples we will look at a table with blog posts called &#039;posts&#039;.
        The &#039;posts&#039; table has four columns: id, title, body, date_created.
        Let&#039;s dive right in and have a look at a simple example.
    </p>

    <pre class="programlisting brush: php">
// Create a select query. $db is a Zend_Db_Adapter object, which we assume
// already exists in your script.
$select = $db-&gt;select()-&gt;from(&#039;posts&#039;)-&gt;order(&#039;date_created DESC&#039;);

// Get a Paginator object using Zend_Paginator&#039;s built-in factory.
$paginator = Zend_Paginator::factory($select);

// Select the second page
$paginator-&gt;setCurrentPageNumber(2);

?&gt;&lt;ul&gt;&lt;?php

// Render each the title of each post for the current page in a list-item
foreach ($paginator as $item) {
    echo &#039;&lt;li&gt;&#039; . $item-&gt;title . &#039;&lt;/li&gt;&#039;;
}

?&gt;&lt;/ul&gt;
</pre>


    <p class="para">
        As you can see, this example is not that different from the previous one.
        The only difference is that you pass a <span class="classname">Zend_Db_Select</span> object to the
        Paginator&#039;s  <span class="methodname">factory()</span> method, rather than an array.
        For more details on how the database adapter makes sure that your query
        is being executed efficiently, see the <span class="classname">Zend_Paginator</span> chapter in the
        reference manual on the DbSelect and DbTableSelect adapters.
    </p>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="learning.paginator.intro.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="learning.paginator.html">Getting Started with Zend_Paginator</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="learning.paginator.control.html">Pagination Control and ScrollingStyles</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="learning.html">Learning Zend Framework</a></li>
  <li class="header up"><a href="learning.paginator.html">Getting Started with Zend_Paginator</a></li>
  <li><a href="learning.paginator.intro.html">Introduction</a></li>
  <li class="active"><a href="learning.paginator.simple.html">Simple Examples</a></li>
  <li><a href="learning.paginator.control.html">Pagination Control and ScrollingStyles</a></li>
  <li><a href="learning.paginator.together.html">Putting it all Together</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>